package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo12_1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scan.nextLine();
        System.out.println("请输入密码:");
        String password = scan.nextLine();
        System.out.println("请输入昵称:");
        String nick = scan.nextLine();
        reg(username,password,nick);
    }
    public static void reg(String username,String password,String nick){
        try(
                Connection conn = DBUtils.getConn()
        ) {
            String sql = "select username from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            String name = rs.getString(1);
            if(name.equals(username)){
                System.out.println("用户名已存在!");
                return;
            }else{
                Statement s = conn.createStatement();
                s.executeUpdate("insert into user(username,password,nick) values('"+username+"','"+password+"','"+nick+"')");
                System.out.println("注册成功!");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
